package cn.kust.tarvelService.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kust.tarvelService.mapper.ArticleCommentMapper;
import cn.kust.tarvelService.pojo.ArticleComment;
import cn.kust.tarvelService.pojo.vo.commentVo;
import cn.kust.tarvelService.service.ArticleCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 评论管理表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-07-26
 */
@Service
public class ArticleCommentServiceImpl extends ServiceImpl<ArticleCommentMapper, ArticleComment> implements ArticleCommentService {

    @Autowired
    private  ArticleCommentMapper commentMapper;

    public void setCommentMapper(ArticleCommentMapper commentMapper) {
        this.commentMapper = commentMapper;
    }

    @Override
    public List<commentVo> getListComment(Long articleID) {
        List<commentVo> comments = this.commentMapper.getCommentByArticleId(articleID);
        List<commentVo> sons = getSons(comments);
        return sons;
    }

    private List<commentVo> getSons(Long parentId){
        return this.commentMapper.getCommentByParentId(parentId);
    }

    private List<commentVo> getSons(List<commentVo> parents){
        if(parents == null || parents.size() == 0){
            return null;
        }
        for (commentVo parent : parents) {
            Long parentId = parent.getCommentId();
            List<commentVo> sonCommentVos = getSons(parentId);
            //递归找子评论
            parent.setChildren(getSons(sonCommentVos));
        }
        return parents;
    }

}
